package com.abou.mvc.controller.customer.product.search;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.abou.constant.product.EProductType;
import com.abou.model.common.product.search.SearchProductsRequest;
import com.abou.model.common.product.search.SearchProductsResult;
import com.abou.mvc.controller.ACommonController;
import com.abou.mvc.controller.ControllerUtils;

/**
 * The abstract controller for the search pages
 * 
 * @author khalid
 */
public abstract class ACustomerSearchController extends ACommonController {
  
  private EProductType productType;
  
  /**
   * The constructor
   * 
   * @param viewName See {@link #getViewName()}.
   * @param exPrevViewName See {@link #getExPrevViewName()}.
   * @param productType See {@link #getProductType()}.
   */
  public ACustomerSearchController(String viewName, String exPrevViewName,
      EProductType productType) {

    super(viewName, exPrevViewName);
    
    this.productType = productType;
  }
  
  /**
   * @return The product type
   */
  public EProductType getProductType() {

    return productType;
  }
  
  /**
   * Sets the product type
   * 
   * @param productType The product type new value
   */
  public void setProductType(EProductType productType) {

    this.productType = productType;
  }
  
  /**
   * Shows the home page.
   * 
   * @return The search result
   */
  @RequestMapping(method = RequestMethod.GET)
  @ModelAttribute(ControllerUtils.SEARCH_RESULT_ATT)
  public SearchProductsResult show(ModelMap model) {

    SearchProductsRequest searchProductsRequest = createProductsRequest();
    model.put(ControllerUtils.SEARCH_REQUEST_ATT, searchProductsRequest);
    
    return getProducts(searchProductsRequest);
  }
  
  /**
   * Receives the form data and handles it.
   * 
   * @param searchProductsRequest The search request data
   * @return The search result
   */
  @RequestMapping(method = RequestMethod.POST)
  @ModelAttribute(ControllerUtils.SEARCH_RESULT_ATT)
  public SearchProductsResult submitForm(
      @ModelAttribute(ControllerUtils.SEARCH_REQUEST_ATT) SearchProductsRequest searchProductsRequest) {

    System.out.println("POST");
    
    return getProducts(searchProductsRequest);
  }
  
  /**
   * @return Creates a search product request
   */
  protected SearchProductsRequest createProductsRequest() {

    SearchProductsRequest searchRequest = new SearchProductsRequest();
    searchRequest.setProductType(productType);
    
    return searchRequest;
  }
  
  /**
   * Gets the products according to the passed request.
   * 
   * @param searchRequest The search request
   * @return The search The search result
   */
  protected SearchProductsResult getProducts(SearchProductsRequest searchRequest) {

    SearchProductsResult searchResult = null;
    
    searchResult =
        getProductManager().listProducts(searchRequest.getName(),
            searchRequest.getDescription(), searchRequest.getProductType(),
            searchRequest.getSortOrder(), searchRequest.getPageIndex(),
            searchRequest.getRowsNbByPage());
    
    return searchResult;
  }
}
